package com.qifan.alb2b.service.resource;


import java.util.ArrayList;
import java.util.LinkedHashMap;
import java.util.List;
import java.util.Map;
import org.springframework.stereotype.Component;
import com.qifan.alb2b.dao.ActivityInfoDao;
import com.qifan.alb2b.dao.CityInfoDao;
import com.qifan.alb2b.domain.ActivityInfoDomain;
import com.qifan.alb2b.domain.CityInfoDomain;
import com.qifan.alb2b.domain.TravelItemInfoDomain;
import com.qifan.alb2b.exception.BusinessException;
import com.qifan.alb2b.service.route.SevenStPrice;
import com.qifan.alb2b.service.route.SixStPrice;
import com.qifan.alb2b.service.vo.Resource;
import com.qifan.alb2b.service.vo.ResourceVo;
import com.qifan.alb2b.service.vo.RouteDetailVo;
import com.qifan.alb2b.service.vo.RouteVo;
import com.qifan.alb2b.service.vo.StPriceTable;
import com.qifan.alb2b.util.Arith;
import com.qifan.alb2b.util.Constants;
import com.qifan.alb2b.util.DataGridModel;
import com.qifan.alb2b.util.ExchangeUtil;
import com.qifan.alb2b.util.Page;

/**
 * 活动资源
 * @author hc24
 *
 */
@Component
public class ActivityResource implements IResource {
	private static String[] header=new String[]{"ID","活动名","时间段","最少人数"};
	
	private ActivityInfoDao activityInfoDao;
	private CityInfoDao cityInfoDao;

	public void setActivityInfoDao(ActivityInfoDao activityInfoDao) {
		this.activityInfoDao = activityInfoDao;
	}

	public void setCityInfoDao(CityInfoDao cityInfoDao) {
		this.cityInfoDao = cityInfoDao;
	}

	public String[] getHeader(){
		return header;
	}
	
	public boolean isSingleSelect(){
		return false;//活动可以多选
	}
	/**
	 * 活动名","时间段","最少人数"
	 */
	public ResTable getResTable(String cityId,int groupType,DataGridModel dgm) {
		//根据城市信息查询活动信息
		ActivityInfoDomain aid=new ActivityInfoDomain();
		aid.setCityId(cityId);
		dgm.setSort("minCount");//设置排序字段
		dgm.setOrder("asc");//设置排序规则
		Page page= activityInfoDao.query(dgm, aid);
		List<ActivityInfoDomain> list=page.getRows();
		
		//获得城市信息&所属国家id
		CityInfoDomain city=cityInfoDao.getCityById(cityId);
		if(city==null)
			throw new BusinessException("设置活动信息失败，该目的城市不存在");		
		String countryId=city.getCountryId();	

		//封装表格每行的数据
		List<Map<String, Object>> datas=new ArrayList<Map<String,Object>>();
		for(int i=0;i<list.size();i++){
			ActivityInfoDomain activity=list.get(i);
			Map<String, Object> map=new LinkedHashMap<String, Object>();
			map.put(header[0], activity.getActivityId());
			map.put(header[1], activity.getActivityName());//活动名称
			map.put(header[2], activity.getBeginTime()+"-"+activity.getEndTime());
			map.put(header[3], activity.getMinCount());
			map.put("查看详情", "<a href='javascript:void(0);' onclick=\"showDetail('"+cityId+"',"+Constants.RES_TYPE_ACTIVITY+",'"+activity.getActivityId()+"')\">查看详情</a>");
			datas.add(map);		
		}
		page.setRows(datas);//重新设置分页的数据
		
		ResTable rt=new ResTable();
		rt.setHeader(header);
		rt.setPage(page);
		rt.setSingleSelect(true);
		return rt;
	}

	/**
	 * 基本描述信息
	 */
	public ResTable getResDetailTable(String cityId,String resId,int groupType) {
		//判断团队类型
		String[] column=new String[]{};
		int[] persons_num = new int[]{};
		if(groupType==Constants.GROUP_TYPE_SIX){
			column=SixStPrice.COLUMN;
			persons_num=SixStPrice.PERSON_NUMS;
		}else if(groupType==Constants.GROUP_TYPE_SEVEN){
			column=SevenStPrice.COLUMN;
			persons_num=SevenStPrice.PERSON_NUMS;
		}
		
		//获得导游信息
		String[] header=new String[]{"人数","价格/人"};
		ActivityInfoDomain activity=activityInfoDao.getActivityById(resId);
		if(activity==null)
			throw new BusinessException("该活动不存在，请联系管理员");
		
		//获得城市信息&所属国家id
		CityInfoDomain city=cityInfoDao.getCityById(cityId);
		if(city==null)
			throw new BusinessException("设置活动信息失败，该目的城市不存在");		
		String countryId=city.getCountryId();	
		
		List<Object[]> datas=new ArrayList<Object[]>();
		for(int i=0;i<persons_num.length;i++){
			Object[] obj=new Object[header.length];
			obj[0]=persons_num[i];//人数
			double gPrice=ExchangeUtil.changeMoney(activity.getgPrice(), countryId);
			double pPrice=Arith.roundCeiling(ExchangeUtil.changeMoney(activity.getpPrice(), countryId),Constants.XIAOSHU);
			double price=0;
			if(persons_num[i]<activity.getMinCount()){
				price=Arith.div(gPrice, activity.getMinCount(), Constants.XIAOSHU);//至少人数时每个人的价格
				price=Arith.add(price, pPrice);
				price=Arith.div(price*activity.getMinCount(), persons_num[i], Constants.XIAOSHU);
			}else{
				price=Arith.div(gPrice, persons_num[i], Constants.XIAOSHU);
				price=price+pPrice;
			}	
			obj[1]="[+"+price+"]";
			datas.add(obj);
		}
		Page page=new Page();
		page.setRows(datas);
		
		ResTable rt=new ResTable();
		rt.setHeader(header);
		rt.setPage(page);	
		rt.addRemark("活动描述", activity.getRemark());
		rt.setSingleSelect(false);
		return rt;
	}

	public void addOrUpdate(RouteVo routeVo, String routeDetailId, String[] resIds) {
		
		//=====查找线路详情=====
		List<RouteDetailVo> detailList= routeVo.getDetailVoList();
		RouteDetailVo routeDetailVo = null;
		for(RouteDetailVo rdv:detailList){
			if(rdv.getRouteDetail().getDetailId().equals(routeDetailId)){
				routeDetailVo=rdv;
				break;
			}
		}
		
		if(routeDetailVo==null)
			return;
		//=====更新活动资源resList=====
		String countryId=routeVo.getRoute().getCountryId();
		List<ResourceVo> resList=routeDetailVo.getResList();
		List<ActivityInfoDomain> oldList=new ArrayList<ActivityInfoDomain>();
		List<ActivityInfoDomain> newList=new ArrayList<ActivityInfoDomain>();
		
		
		if(resIds==null||resIds.length==0){//①代表清除
			for(int i=0;i<resList.size();i++){
				ResourceVo rv=resList.get(i);
				if(rv.getResType()==Constants.RES_TYPE_ACTIVITY){
					oldList.add((ActivityInfoDomain) rv.getResource());//增加旧的
					ActivityInfoDomain newActivity=new ActivityInfoDomain();
					newActivity.setActivityId("");
					newActivity.setpPrice(0);
					newActivity.setgPrice(0);
					newActivity.setMinCount(1);
					newList.add(newActivity);//增加新的，实际上就是个“0”对象
					resList.remove(i);//移除
					i--;
				}//end if
			}//end for
		}else{//②代表增加或者更新
			//首先把旧的全部移除
			for(int i=0;i<resList.size();i++){
				ResourceVo rv=resList.get(i);
				if(rv.getResType()==Constants.RES_TYPE_ACTIVITY){
					oldList.add((ActivityInfoDomain) rv.getResource());//增加旧的
					resList.remove(i);//移除
					i--;
				}//end if
			}//end for
			//其次把新的全部加进来
			List<ActivityInfoDomain> list=activityInfoDao.getActivityByIds(resIds);
			for(int n=0;n<list.size();n++){
				ActivityInfoDomain newActivity=list.get(n);
				ResourceVo rv=new ResourceVo();
				rv.setIsStd(1);
				rv.setResId(newActivity.getActivityId());
				newActivity.setgPrice((float) ExchangeUtil.changeMoney(newActivity.getgPrice(),countryId));//在这里就把价格转化成人民币!!!
				newActivity.setpPrice((float) ExchangeUtil.changeMoney(newActivity.getpPrice(),countryId));//在这里就把价格转化成人民币!!!
				rv.setResource(newActivity);
				rv.setResType(Constants.RES_TYPE_ACTIVITY);
				resList.add(rv);
			}
			newList=list;
		}
		
		
		//=====更新标配价格表StTable=====
		StPriceTable spt=routeVo.getStPriceTable();
		List<double[]> datas= spt.getDatas();
		String[] rowType=spt.getRowTypes();
		int[] persons_num = null;
		int groupType=routeVo.getGroupType();
		if(groupType==Constants.GROUP_TYPE_SIX){
			persons_num=SixStPrice.PERSON_NUMS;
		}else if(groupType==Constants.GROUP_TYPE_SEVEN){
			persons_num=SevenStPrice.PERSON_NUMS;
		}
		
		for(int i=0;i<rowType.length;i++){//每行	
			double[] rows=datas.get(i);
			for(int j=0;j<rows.length;j++){//没列
				double oldPrice=0;
				double newPrice=0;
				for(int n=0;n<oldList.size();n++){//旧的总的平均价格
					ActivityInfoDomain oldActivity=oldList.get(n);
					if(persons_num[j]<oldActivity.getMinCount()){
						double price=Arith.div(oldActivity.getgPrice(), oldActivity.getMinCount(), Constants.XIAOSHU);
						price=price+Arith.roundCeiling(oldActivity.getpPrice(), Constants.XIAOSHU);
						oldPrice=oldPrice+Arith.div(price*oldActivity.getMinCount(), persons_num[j], Constants.XIAOSHU);
					}else{
						double price=Arith.div(oldActivity.getgPrice(), persons_num[j], Constants.XIAOSHU);
						oldPrice=oldPrice+Arith.add(price, Arith.roundCeiling(oldActivity.getpPrice(), Constants.XIAOSHU));
					}
				}
				for(int n=0;n<newList.size();n++){//新的总的平均价格
					ActivityInfoDomain newActivity=newList.get(n);
					if(persons_num[j]<newActivity.getMinCount()){
						double price=Arith.div(newActivity.getgPrice(), newActivity.getMinCount(), Constants.XIAOSHU);
						price=price+Arith.roundCeiling(newActivity.getpPrice(), Constants.XIAOSHU);
						newPrice=newPrice+Arith.div(price*newActivity.getMinCount(), persons_num[j], Constants.XIAOSHU);
					}else{
						double price=Arith.div(newActivity.getgPrice(), persons_num[j], Constants.XIAOSHU);
						newPrice=newPrice+Arith.add(price, Arith.roundCeiling(newActivity.getpPrice(), Constants.XIAOSHU));
					}
				}
				double diffPrice=Arith.sub(newPrice,oldPrice);//差价
				rows[j]=Arith.add(rows[j], diffPrice);
				
			}//end for j			
		}//end for i
	}

	public void calcPrice(TravelItemInfoDomain travelItem, Resource resource,
			int personNum) {
		ActivityInfoDomain activity=(ActivityInfoDomain) resource;
		double price=0;
		double pprice=Arith.roundCeiling(activity.getgPrice(), Constants.XIAOSHU);
		if(personNum<activity.getMinCount()){
			double gprice=Arith.div(activity.getgPrice(), activity.getMinCount(), Constants.XIAOSHU);
			price=price+pprice;
			price=Arith.div(price*activity.getMinCount(), personNum,Constants.XIAOSHU);
		}else{
			double gprice=Arith.div(activity.getgPrice(), personNum, Constants.XIAOSHU);
			price=Arith.add(price, gprice);
		}
		
		travelItem.setPrice(price);
		travelItem.setRemark("团队价格："+activity.getgPrice()+";每人价格："+activity.getpPrice());
	}
	
	
}
	
	 


